package java_web;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 最简单的web，
 */
public class demo_01_simple_socket {
    public static void main(String[] arge) throws IOException {
        new Thread(new ServerSimple()).start();
        new Thread(new ClientSimple()).start();
    }
}

/**
 * 基本的服务器端，用于接受信息
 */
class ServerSimple implements Runnable{
    @Override
    public void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8010);
            System.out.println("服务器启动...");
            while (true){
                Socket socket = serverSocket.accept(); // 阻塞至有请求
                System.out.println("出现请求");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // 处理请求
                        System.out.println("线程:"+Thread.currentThread().getName()+" "+socket);
                        try {
                            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                            while (true){
                                br.mark(1);
                                // 首先读取两个字节表示的长度
                                int first = br.read();
                                //如果读取的值为-1 说明到了流的末尾，Socket已经被关闭了，此时将不能再去读取
                                if(first==-1){
                                    break;
                                }
                                br.reset();
                                //读取客户端发送来的消息
                                String line;
                                StringBuilder sb = new StringBuilder();
                                while ((line = br.readLine()) != null && "end".equals(line)) {
                                    //注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
                                    sb.append(((char)first)+line);
                                }
                                System.out.println("取得的信息："+line);
                                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                                bw.write(line+"\n");
                                bw.flush();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


/**
 * 基本的客户端端，每隔一秒发送信息
 */
class ClientSimple implements Runnable{
    @Override
    public void run() {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            socket = new Socket("localhost",8010);
            System.out.println("客户端启动...");
            outputStream = socket.getOutputStream();
            while (true){
                Thread.sleep(1000);
                outputStream.write("测试信息123 \nend\n".getBytes());
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}